<template>
  <div class="container">
    <el-card class="box-card" shadow="always">
      <el-row :gutter="24">
        <el-col :span="24">
          <el-form inline @submit.native.prevent="search">
            <el-form-item label="搜索">
              <el-input
                v-model="searchForm.queryConditions"
                clearable
                placeholder=""
                prefix-icon="el-icon-search"
                size="medium"
              />
            </el-form-item>
            <el-form-item label="标案类型">
              <el-select
                v-model="productTypeList.productType"
                clearable
                placeholder="全部"
                @change="productTypeSelected"
              >
                <el-option
                  v-for="item in productTypeList"
                  :key="item.id"
                  :value="item.id"
                  :label="item.productType"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="标案状态">
              <el-select
                v-model="searchForm.tenderState"
                clearable
                placeholder="全部"
                style="width: 120px"
                @change="stateSelected"
              >
                <el-option value="4" label="竞价中" />
                <el-option value="5" label="本轮竞价结束" />
                <el-option value="6" label="待定标" />
                <el-option value="7" label="已定标" />
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button native-type="submit" size="medium" type="primary"
                >搜索</el-button
              >
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
      <div>
        <el-table
          :data="list"
          border
          style="width: 100%"
          @selection-change="handleSelectChange"
          @row-click="handleRowChange"
          v-loading="bidApplyLoading"
          ref="tb"
        >
          <el-table-column type="selection" width="55" align="center" />

          <el-table-column
            type="index"
            width="50"
            label="序号"
            align="center"
            fixed="left"
          />

          <el-table-column prop="tenderCode" label="标案编码" align="center" width="250" fixed/>
          <el-table-column prop="tenderName" width="150" label="标案名称" align="center" />
          <el-table-column label="招标方式" width="150" align="center">
            <template slot-scope="scope">
              <el-tag type="warning">{{ scope.row.tenderMode === 0 ? '公开招标':'非公开招标'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="显示排名" width="150" align="center">
            <template slot-scope="scope">
              <el-tag type="danger">{{ scope.row.tenderRanking === true ? '显示':'不显示'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="显示最低价" width="150" align="center">
            <template slot-scope="scope">
              <el-tag type="danger">{{ scope.row.minimumPrice === true ? '显示':'不显示'}}</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="purchaser" width="150" label="管理人" align="center" />
          <el-table-column prop="mobilePhone" width="150"  label="联系电话" align="center" />
          <el-table-column prop="startTime" width="150" label="开始时间" align="center" />
          <el-table-column label="当前状态" width="150" align="center" fixed="right">
            <template slot-scope="scope">
              <el-tag type="warning">
                {{ stateConvert(scope.row.tenderState) }}
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="操作" width="100" align="center" fixed="right">
            <template slot-scope="scope">
              <el-tooltip content="查看标案详细信息" placement="top">
                <el-button
                  size="mini"
                  icon="el-icon-search"
                  circle
                  @click="showDialog(scope.row)"
                />
              </el-tooltip>
              <el-tooltip
                content="竞标结果"
                placement="top"
                v-if="scope.row.tenderState === '7'"
              >
                <el-button
                  size="mini"
                  type="success"
                  icon="el-icon-news"
                  circle
                  @click="competeResult(scope.row)"
                />
              </el-tooltip>
              <!-- <el-tooltip content="删除标案" placement="top">
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="deleteProduct(scope.row)"
                />
              </el-tooltip> -->
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          :current-page="paging.current"
          :page-sizes="paging.pageSizes"
          :page-size="paging.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paging.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <Dialog
        :visible="tenderInfoVisible"
        :data="tenderInfo"
        @cancel="cancel"
      />
    </el-card>

    <el-drawer
      title="竞标"
      :visible.sync="dialog"
      destroyOnClose
      direction="rtl"
      ref="drawer"
      size="60%"
    >
      <bid-tender :tenderInfo="info" :products="productArray" :isRanking="isRanking" :isMinimumPrice="isMinimumPrice" />
    </el-drawer>

    <el-drawer
      title="结果公告"
      :visible.sync="resultDialog"
      destroyOnClose
      direction="ltr"
      ref="drawer"
      size="60%"
    >
      <compete-result :tenderInfo="info" :products="productResultArray" />
    </el-drawer>
  </div>
</template>

<script>
import {
  getAllTenderCanBeCompeted,
  getTenderProductCanBeCompeted,
} from "@/api/supplierTender.js";
import { getTenderAllInfoByTenderId } from "@/api/purchaser-tender.js";
import { getProductTypeBriefInfos } from "@/api/productType";
import Dialog from "../purchaser-tender/noApprove/components/Dialog";
import CompeteResult from "./components/compete-result.vue";
import BidTender from "./components/bid-tender.vue";
import { getTenderProduct } from "@/api/purchaserTender";

export default {
  name: "ManagerTenderVerify",
  components: { Dialog, BidTender, CompeteResult },
  data() {
    return {
      searchForm: {
        queryConditions: "",
        typeId: "",
        tenderState: "",
      },
      productTypeList: [],
      list: [],
      paging: {
        total: 0,
        current: 1,
        pageSizes: [5, 10, 15],
        size: 5,
      },
      tenderInfoVisible: false,
      bidApplyLoading: false,
      tenderInfo: {},
      dialogTitle: "",
      multipleSelection: [], //复选框选中的所有数据
      index: 0, // 0 添加；1 编辑；2 查看信息
      // headers: {
      //   Authorization: getToken(),
      // },
      dialog: false,
      resultDialog: false,
      info: {
        tenderName: "",
        tenderCode: "",
        tenderId: "",
      },
      productArray: [],
      productResultArray: [],
      isRanking: false,
      isMinimumPrice: false,
    };
  },
  created() {
    this.bidApplyLoading = true;
    this.getData();
    this.getProductType();
  },
  methods: {
    stateConvert(val) {
      if (val === "4") {
        return "竞价中";
      } else if (val === "5") {
        return "本轮竞价结束";
      } else if (val === "6") {
        return "待定标";
      } else if (val === "7") {
        return "已定标";
      }
    },
    async getProductType() {
      const result = await getProductTypeBriefInfos();
      this.productTypeList = result.data;
      // console.log('@@@@',this.productTypeList)
    },
    search() {
      this.paging.current = 1;
      this.bidApplyLoading = true;
      this.getData();
    },
    stateSelected(val) {
      this.searchForm.tenderState = val;
      this.search();
    },
    getData() {
      const form = {
        ...this.searchForm,
        page: this.paging.current,
        pageSize: this.paging.size,
        supplierId: this.$store.getters.accountId,
      };

      getAllTenderCanBeCompeted(form)
        .then(({ data }) => {
          console.log(data);
          this.list = data.supplierTenderInfos;
          //this.paging.size = data.size
          this.paging.total = data.totalCount;
          this.bidApplyLoading = false;
        })
        .catch((err) => {
          // 如果没有数据就显示为空
          this.list = [];
          // 返还每页显示的条数
          //this.paging.size = res.data.size;
          this.paging.total = 0;
          this.bidApplyLoading = false;
        });
    },

    // 获取到选中的所有数据
    handleSelectChange(val) {
      if (val.length > 1) {
        const del_row = val.shift();
        this.$refs.tb.toggleRowSelection(del_row, false);
      }
    },

    handleRowChange(row, column, event) {
      if (column.label === "操作") {
        console.log("不走行点击事件");
      } else {
        if (row.tenderState != "4") {
          this.$notify({
            title: "警告",
            message: "当前时间段无法参与竞价！",
            type: "warning",
          });
          return;
        }
        if (this.multipleSelection[0] == row) {
          // 选中的是已有的 取消选中
          this.multipleSelection = [];
          this.$refs["tb"].clearSelection();
        } else {
          this.multipleSelection = [row];
          this.$refs["tb"].clearSelection();
          this.$refs["tb"].toggleRowSelection(row, true);
        }
        this.info.tenderName = row.tenderName;
        this.info.tenderCode = row.tenderCode;
        this.info.tenderId = row.id;
        this.isRanking = row.tenderRanking;
        this.isMinimumPrice = row.minimumPrice;
        this.dialog = true;
        this.initData(row.id);
      }
    },
    initData(tenderId) {
      const data = {
        tenderId: tenderId,
        supplierId: this.$store.getters.accountId,
      };
      getTenderProductCanBeCompeted(data).then((res) => {
        this.productArray = res.data;
      });
    },

    handleSizeChange(val) {
      this.paging.current = 1;
      this.paging.size = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.paging.current = val;
      this.getData();
    },
    showDialog(row) {
      this.dialog = false;
      this.index = 2;
      this.getTenderInfoById(row.id);
    },
    getTenderInfoById(tenderId) {
      getTenderAllInfoByTenderId({ tenderId: tenderId })
        .then((res) => {
          if (res.code === 200) {
            this.tenderInfo = res.data;
            if (this.index === 2) {
              this.tenderInfoVisible = true;
            }
          }
        })
        .catch((err) => {
          this.$message("warning", "正在加载中！");
        });
    },
    productTypeSelected(val) {
      this.searchForm.typeId = val;
      this.search();
    },

    competeResult(row) {
      this.info.tenderName = row.tenderName;
      this.info.tenderCode = row.tenderCode;
      this.info.tenderId = row.id;
      this.resultDialog = true;
      this.initResultProduct(row.id);
    },
    initResultProduct(tenderId) {
      const data = {
        tenderId: tenderId,
      };
      getTenderProduct(data).then((res) => {
        this.productResultArray = res.data;
      });
    },
    // 对象的深拷贝
    // 对象进行深拷贝，否则“编辑”框内修改内容也会影响到页面
    extendCopy(p) {
      var c = {};
      for (var i in p) {
        c[i] = p[i];
      }
      return c;
    },

    cancel() {
      if (this.index === 2) {
        this.tenderInfoVisible = false;
      } else {
        this.editDialogVisible = false;
      }
      this.index = 0;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
  text-align: right;
}
.btn {
  float: right;
  margin-left: 20px;
}
</style>
